package com.zz.nondatain.view.activity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.zz.nondatain.R;
import com.zz.nondatain.base.BaseActivity;
import com.zz.nondatain.databinding.ActivityAppealDetailBinding;

/**
 * 申诉详情页面
 */
public class AppealDetailActivity extends BaseActivity<ActivityAppealDetailBinding> {
    
    // 申诉详情数据
    private String appealTime;
    private String appealReason;
    private String occurrenceTime;
    private String factReason;
    private String contactPhone;
    private String status;

    @Override
    protected void initView() {
        // 获取申诉详情数据
        Intent intent = getIntent();
        if (intent != null) {
            appealTime = intent.getStringExtra("appeal_time");
            appealReason = intent.getStringExtra("appeal_reason");
            occurrenceTime = intent.getStringExtra("occurrence_time");
            factReason = intent.getStringExtra("fact_reason");
            status = intent.getStringExtra("status");
            contactPhone = intent.getStringExtra("contact_phone") != null ? 
                    intent.getStringExtra("contact_phone") : "15055688236";
        }
        
        // 设置数据到视图
        updateUI();
    }

    @Override
    protected void initData() {
        // 不需要额外初始化数据
    }

    @Override
    protected void initObservers() {
        setupListeners();
    }

    @Override
    protected ActivityAppealDetailBinding getViewBinding() {
        return ActivityAppealDetailBinding.inflate(getLayoutInflater());
    }
    
    /**
     * 设置监听器
     */
    private void setupListeners() {
        // 返回按钮点击事件
        binding.btnBack.setOnClickListener(v -> finish());
        
        // 撤销按钮点击事件
        binding.btnCancelAppeal.setOnClickListener(v -> showCancelConfirmDialog());
        
        // 证明材料图片点击事件
        binding.ivEvidence.setOnClickListener(v -> {
            // 这里可以实现查看大图功能，为简化起见，仅显示Toast提示
            showToast("查看大图");
        });
    }
    
    /**
     * 更新UI
     */
    private void updateUI() {
        if (appealTime != null) binding.tvAppealTime.setText(appealTime);
        if (appealReason != null) binding.tvAppealReason.setText(appealReason);
        if (occurrenceTime != null) binding.tvOccurrenceTime.setText(occurrenceTime);
        if (factReason != null) binding.tvFactReason.setText(factReason);
        if (contactPhone != null) binding.tvContactPhone.setText(contactPhone);
        
        // 设置状态及颜色
        if (status != null) {
            binding.tvAppealStatus.setText(status);
            
            switch (status) {
                case "审核中":
                    binding.tvAppealStatus.setTextColor(getResources().getColor(android.R.color.holo_blue_light));
                    // 审核中可以撤销
                    binding.btnCancelAppeal.setVisibility(View.VISIBLE);
                    break;
                case "已通过":
                    binding.tvAppealStatus.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
                    // 已通过不能撤销
                    binding.btnCancelAppeal.setVisibility(View.GONE);
                    break;
                case "已拒绝":
                    binding.tvAppealStatus.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                    // 已拒绝不能撤销
                    binding.btnCancelAppeal.setVisibility(View.GONE);
                    break;
                default:
                    binding.tvAppealStatus.setTextColor(getResources().getColor(android.R.color.darker_gray));
                    binding.btnCancelAppeal.setVisibility(View.GONE);
                    break;
            }
        }
        
        // 设置示例图片（实际应从网络或本地加载）
        // 这里简化处理，使用占位图
        binding.ivEvidence.setImageResource(android.R.drawable.ic_menu_gallery);
    }
    
    /**
     * 显示撤销确认对话框
     */
    private void showCancelConfirmDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("撤销申诉")
                .setMessage("确定要撤销此申诉吗？")
                .setPositiveButton("确定", (dialog, which) -> {
                    // 执行撤销操作
                    cancelAppeal();
                })
                .setNegativeButton("取消", null)
                .show();
    }
    
    /**
     * 撤销申诉
     */
    private void cancelAppeal() {
        // 这里应该发送撤销请求到服务器
        // 为简化起见，直接显示成功提示并返回
        showToast("申诉已撤销");
        
        // 返回结果并返回上一页
        Intent result = new Intent();
        result.putExtra("is_canceled", true);
        setResult(RESULT_OK, result);
        finish();
    }
} 